//
//  JXPostsListCtl.m
//  JiaoX
//
//  Created by yinqy on 15/3/3.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXPostsListCtl.h"
#import "JXTitleButton.h"
#import "PopMenuListView.h"
#import "PostsListTitleView.h"
#import "JXPopMenu.h"
#import "PopMenuBean.h"
#import "JXPostsChildListCtl.h"
#import "JXNewTopicCtl.h"
#import "ForumDataTool.h"
#define TITLE_BTN_HEIGHT 45
#define SEND_POSTS_BTN_HEIGHT SCALEX(40)

@interface JXPostsListCtl ()<PopMenuListViewDelegate,JXPopMenuDelegate,UIScrollViewDelegate,JXPostsChildListCtlDelegate>
@property (nonatomic,copy)NSString *modelId;
@property (nonatomic,strong)BBSModel *currentModel;
@property (nonatomic,strong)PostsListTitleView *titleView;
@property (nonatomic,strong)NSArray *modelArray;
@property (nonatomic,strong)JXPopMenu *menu;
@property (nonatomic,strong)UIImageView *accessView;

@property (nonatomic,strong)UIButton *freshBtn;
@property (nonatomic,strong)UIButton *essenceBtn;
@property (nonatomic,strong)UIButton *currentBtn;;
@property (nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)NSMutableArray *childController;

@property (nonatomic,strong)UIButton *sentPostsBtn;



@end

@implementation JXPostsListCtl

-(instancetype)initWithModelId:(NSString *)modelId{
    self = [super init];
    if (self) {
        self.modelId = modelId;
    }
    return self;
}

-(instancetype)initWithBBSmodel:(BBSModel *)model modelList:(NSArray *)array{
    self = [super init];
    if (self) {
        self.currentModel = model;
        self.modelArray = array;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:JXGlobalColorBg];
    
    if (self.currentModel) {
        [self setTitleBtn:self.currentModel.name];
        [self initViews];
    }else{
        //去请求
        MBProgressHUD *HUD = [DialogUtils showLoadingWithView:self.view];
        [ForumDataTool getModeList:^(BBSHomeData *result) {
            [HUD hide:YES];
            if (!result.code) {
                self.modelArray = result.modelList;
                
                for (BBSModel *model in result.modelList) {
                    if ([[NSString stringWithFormat:@"%@",model.modelId] isEqualToString:self.modelId]) {
                        self.currentModel = model;
                        break;
                    }
                }
                [self setTitleBtn:self.currentModel.name];
                [self initViews];
            }else{
                [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
            }
        } failure:^(NSError *error) {
            [HUD hide:YES];
            [DialogUtils showTipsWithView:self.navigationController.view String:@"加载失败"];
        }];
    }
}

-(void)initViews{
    self.childController = [NSMutableArray new];
    self.titleView = [[PostsListTitleView alloc]init];
    [self.titleView setY:NavHeight];
    [self.titleView setModel:self.currentModel];
    [self.view addSubview:self.titleView];
    
    
    _accessView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_order_access"]];
    
    self.freshBtn = [self getTitleBtnWithTitle:@"新鲜" point:CGPointMake(0, CGRectGetMaxY(self.titleView.frame))];
    [self.freshBtn setTag:100];
    [self titleBtnClick:self.freshBtn];
    self.essenceBtn = [self getTitleBtnWithTitle:@"精华" point:CGPointMake(JXScreenW/2,CGRectGetMaxY(self.titleView.frame))];
    [self.essenceBtn setTag:101];
    
    [self.view addSubview:self.freshBtn];
    [self.view addSubview:self.essenceBtn];
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.freshBtn.frame), JXScreenW, self.view.height-NavHeight-TITLE_BTN_HEIGHT-self.titleView.height)];
    _scrollView = scrollView;
    [self.view addSubview:scrollView];
    
    for (int i=0; i<2; i++) {
        JXPostsChildListCtl *ctr = [[JXPostsChildListCtl alloc]initWithBBSModel:self.currentModel modulePostsType:i];
        [ctr setDelegate:self];
        [ctr.view setFrame:CGRectMake(i*JXScreenW, 0, JXScreenW, _scrollView.height)];
        [_childController addObject:ctr];
        [_scrollView addSubview:ctr.view];
    }
    [_scrollView setContentSize:CGSizeMake(JXScreenW*_childController.count, _scrollView.height)];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    
    _sentPostsBtn = [[UIButton alloc]initWithFrame:CGRectMake((JXScreenW-SEND_POSTS_BTN_HEIGHT)/2, self.view.height-SEND_POSTS_BTN_HEIGHT-10, SEND_POSTS_BTN_HEIGHT, SEND_POSTS_BTN_HEIGHT)];
    [_sentPostsBtn setBackgroundColor:[UIColor blackColor]];
    [_sentPostsBtn setAlpha:0.7];
    [_sentPostsBtn.layer setCornerRadius:_sentPostsBtn.width/2];
    [_sentPostsBtn.layer setMasksToBounds:YES];
    [_sentPostsBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [_sentPostsBtn setImage:[UIImage imageNamed:@"navigationbar_publish"] forState:UIControlStateNormal];
    [_sentPostsBtn setImage:[UIImage imageNamed:@"navigationbar_publish_highlighted"] forState:UIControlStateHighlighted];
    [_sentPostsBtn addTarget:self action:@selector(sendNewPosts) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:_sentPostsBtn aboveSubview:_scrollView];
}

-(void)sendNewPosts{
    NSInteger index = 0;
    for (BBSModel *model in self.modelArray) {
        if ([self.currentModel isEqual:model]) {
            break;
        }
        index++;
    }
    JXNewTopicCtl *ctl = [[JXNewTopicCtl alloc]initWithModelList:self.modelArray currentModeIndex:index];
    [self.navigationController pushViewController:ctl animated:YES];
}


#pragma mark 设置顶部title，相关操作逻辑
/**
 *  设置顶部title
 */
-(void)setTitleBtn:(NSString *)title{
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:title forState:UIControlStateNormal];
    [button.titleLabel setFont:JXNavigationTitleFont];
    [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
    CGSize titleLabelSize = [button.titleLabel.text sizeWithFont:JXNavigationTitleFont];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"navigationbar_arrow_up"]];
    [button setWidth:titleLabelSize.width];
    [button setHeight:titleLabelSize.height+imageView.height*3];
    [imageView setY:titleLabelSize.height+imageView.height*1.5];
    [imageView setX:(button.width-imageView.width)/2];
    [imageView setTag:44];
    
    [button addSubview:imageView];
    
    self.navigationItem.titleView = button;
}

-(void)titleClick:(UIButton *)btn{
    NSMutableArray *array = [NSMutableArray array];
    for (BBSModel *model in self.modelArray) {
        [array addObject:[[PopMenuBean alloc] initWithTitle:model.name bageNum:0]];
    }
    PopMenuListView *listView = [[PopMenuListView alloc]initWithItemArray:array];
    listView.delegate = self;
    self.menu = [JXPopMenu popMenuWithContentView:listView];
    self.menu.arrowPosition = JXPopMenuArrowPositionCenter;
    self.menu.delegate = self;
    self.menu.dimBackground = YES;
    [self setBtnSelect:YES btn:btn];
    [self.menu showInRect:CGRectMake((JXScreenW-listView.width)/2, NavHeight-3, listView.width, listView.height)];
}

- (void)popMenuDidDismissed:(JXPopMenu *)popMenu{
    UIButton *btn = (UIButton *)self.navigationItem.titleView;
    [self setBtnSelect:NO btn:btn];
}

-(void)setBtnSelect:(BOOL)select btn:(UIButton *)btn{
     UIImageView *image = (UIImageView *)[btn viewWithTag:44];
    if (select) {
        [image setImage:[UIImage imageNamed:@"navigationbar_arrow_down"]];
    }else{
        [image setImage:[UIImage imageNamed:@"navigationbar_arrow_up"]];
    }
}

- (void)popMenuItemClick:(NSUInteger)index{
    
    BBSModel *model = self.modelArray[index];
    self.currentModel = model;
    [self.titleView setModel:self.currentModel];
    [_childController makeObjectsPerformSelector:@selector(setBBSModel:) withObject:model];
    [self setTitleBtn:self.currentModel.name];
    
    [self.menu dismiss];
}

-(UIButton *)getTitleBtnWithTitle:(NSString *)title point:(CGPoint)point{
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor whiteColor]];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:15]]];
    [btn setTitleColor:JXThemeColor forState:UIControlStateSelected];
    [btn setTitleColor:JXColor(77, 77, 77) forState:UIControlStateNormal];
    [btn setFrame:CGRectMake(point.x, point.y, JXScreenW/2, TITLE_BTN_HEIGHT)];
    [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, TITLE_BTN_HEIGHT-0.5, JXScreenW/2, 0.5)];
    [view setBackgroundColor:JXColor(204, 204, 204)];
    
    [btn addSubview:view];
    return btn;
}

-(void)titleBtnClick:(UIButton *)btn{
    if(_currentBtn.tag ==btn.tag){
        return;
    }else{
        btn.selected = YES;
        _currentBtn.selected = false;
        _currentBtn = btn;
        [self setScrollCurrentPageNum:(int)btn.tag-100];
        
        [_accessView removeFromSuperview];
        _accessView.y = TITLE_BTN_HEIGHT-_accessView.height;
        _accessView.x = (JXScreenW/2-_accessView.width)/2;
        [btn addSubview:_accessView];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //当前页数
    int pageNum = (scrollView.contentOffset.x+scrollView.width*0.5)/scrollView.width;
    [self setScrollCurrentPageNum:pageNum];
    UIButton *btn = (UIButton *)[self.view viewWithTag:pageNum+100];
    [self titleBtnClick:btn];
}

-(void)setScrollCurrentPageNum:(int)pageNum{
    CGPoint p = CGPointMake(pageNum*_scrollView.frame.size.width, 0);
    [_scrollView setContentOffset:p];
}

-(void)scrollToSecondPage{
    if (self.titleView.y==NavHeight) {
        self.scrollView.height = self.view.height-TITLE_BTN_HEIGHT;
        ((UIViewController *)[_childController firstObject]).view.height = self.view.height-TITLE_BTN_HEIGHT-NavHeight;
        ((UIViewController *)[_childController lastObject]).view.height = self.view.height-TITLE_BTN_HEIGHT-NavHeight;
        [UIView animateWithDuration:0.3 animations:^{
            self.titleView.y = -self.titleView.height;
            self.freshBtn.y = NavHeight;
            self.essenceBtn.y=NavHeight;
            self.scrollView.y=TITLE_BTN_HEIGHT+NavHeight;
        }];
    }
}

-(void)scrollUp{
    //用户向上滑动。隐藏发帖子按钮
    [UIView animateWithDuration:0.3 animations:^{
        [_sentPostsBtn setY:self.view.height];
    }];
}

-(void)scrollDown{
    //用户向下滑动。显示发帖子的按钮
    [UIView animateWithDuration:0.3 animations:^{
        [_sentPostsBtn setY:self.view.height-SEND_POSTS_BTN_HEIGHT-10];
    }];
}

@end
